package com.jvyou.subject.application.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.jvyou.subject.application.convert.SubjectCategoryDtoConverter;
import com.jvyou.subject.application.dto.SubjectCategoryDto;
import com.jvyou.subject.common.entity.Result;
import com.jvyou.subject.domain.entity.SubjectCategoryBo;
import com.jvyou.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 橘柚
 * @since 2024/9/26 19:58
 */
@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 添加题目分类
     *
     * @param subjectCategoryDto 题目分类
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            // 这里为什么要先判断 isInfoEnabled 呢
            // 通过 JSON序列化的时候，即使配置了全局的日志级别为 debug，不会输出 INFO级别的日志，后面的序列化也会执行
            // 这很浪费性能，所以先判断一下
            if (log.isInfoEnabled()) {
                log.info("SubjectCategory.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            // 使用 guava 校验参数不为空
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryName(), "题目分类名称不为空");
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(), "题目分类不为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "题目分类父级ID不为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.dtoToBo(subjectCategoryDto);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategory.add.error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询所有一级分类
     *
     * @return 一级分类
     */
    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDTO) {
        try {
            log.info("SubjectCategory.queryPrimaryCategory");
            SubjectCategoryBo subjectCategoryBO = SubjectCategoryDtoConverter.INSTANCE.dtoToBo(subjectCategoryDTO);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDto> subjectCategoryDtos = SubjectCategoryDtoConverter.INSTANCE.boToDto(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtos);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询父级分类下的子级分类
     *
     * @param parentId 父级分类ID
     * @return 子级分类
     */
    @GetMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(Long parentId) {
        try {
            log.info("SubjectCategory.queryCategoryByPrimary.parentId:{}", parentId);
            Preconditions.checkNotNull(parentId, "题目分类父级ID不为空");
            SubjectCategoryBo subjectCategoryBo = new SubjectCategoryBo();
            subjectCategoryBo.setParentId(parentId);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subjectCategoryDtos = SubjectCategoryDtoConverter.INSTANCE.boToDto(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtos);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategory.update.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }

            Preconditions.checkNotNull(subjectCategoryDto.getId(), "题目分类ID不为空");

            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.dtoToBo(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBo);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategory.update.error:{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.id:{}", id);
            }
            Preconditions.checkNotNull(id, "题目分类ID不为空");
            Boolean result = subjectCategoryDomainService.delete(id);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectCategoryController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除分类失败");
        }
    }

}
